package com.leyou.user.web;


import com.leyou.auth.dto.UserDetail;
import com.leyou.auth.utils.UserContext;
import com.leyou.user.dto.UserDTO;
import com.leyou.user.dto.UserRequestDTO;
import com.leyou.user.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/info")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }


    /**
     * 发送短信验证码
     * @param phone
     * @return
     */
    @PostMapping("/code")
    public ResponseEntity<Void> sendMsg(@RequestParam("phone") String phone) {

        this.userService.sendMsg(phone);
        return ResponseEntity.ok().build();
    }

    /**
     * 查验用户信息是否存下
     * @param data
     * @param type
     * @return
     */
    @GetMapping("/exists/{data}/{type}")
     public ResponseEntity<Boolean>checkExists(
                @PathVariable("data")String data,
                @PathVariable("type")Integer type){
        return ResponseEntity.ok(this.userService.checkExists(data,type));

    }

    /**
     * 用户注册
     * @param userRequestDTO  Valid 表示开启校验支持
     * @return
     */
    @PostMapping
    public ResponseEntity<Void>addUser( @Valid UserRequestDTO userRequestDTO){

        this.userService.addUser(userRequestDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据用户名或密码查询用户信息
     *
     * @param username
     * @param password
     * @return
     */

    @GetMapping
    public ResponseEntity<UserDTO> queryUserWithPass(
            @RequestParam("username") String username,
            @RequestParam("password") String password) {

        return ResponseEntity.ok(this.userService.queryUserWithPass(username, password));
    }

    @GetMapping("/me")
    public ResponseEntity<UserDetail>showUserInfo(){

        return ResponseEntity.ok(UserContext.getUser());
    }

}
